package com.eltonkent.dashboard.service.requesthandler;

import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.eltonkent.dashboard.constants.ApplicationConstants;
import com.eltonkent.dashboard.data.container.Container;
import com.eltonkent.dashboard.data.container.GeoCodeContainer;
import com.eltonkent.dashboard.service.ServiceManagerImpl;
import com.eltonkent.dashboard.service.helper.HttpServiceHelper;
import com.eltonkent.dashboard.service.responsehandler.GoogleReverseCodeHandler;
import com.eltonkent.dashboard.util.gps.WebResponseNotificationListener;
import com.eltonkent.dashboard.util.logger.Logger;

public final class GoogleReverseCodeRequest extends HttpRequest {

	private final double latitude;
	private final double longitude;
	private final boolean isWeather;

	public GoogleReverseCodeRequest(WebResponseNotificationListener listener,
			double latitude, double longitude, boolean isForWeather) {
		super(listener);
		this.latitude = latitude;
		this.longitude = longitude;
		this.isWeather = isForWeather;
	}

	@Override
	public GeoCodeContainer call() throws Exception {
		HttpEntity httpEntity = httpServiceHelper.execute();
		InputStream is = httpEntity.getContent();
		GoogleReverseCodeHandler xmlHandler = new GoogleReverseCodeHandler();
		GeoCodeContainer container = xmlHandler.deserialize(is);
		return container;
	}

	@Override
	public void postExecute(Container container, Throwable throwable) {
		if (container != null) {
			GeoCodeContainer reverseContainer = (GeoCodeContainer) container;
			Logger.d("DBA", "Google Reverse-Code Complete!"
					+ reverseContainer.toString());
			reverseContainer.setLatitude(latitude);
			reverseContainer.setLongitude(longitude);
			if (reverseContainer.getLocationAddress() != null) {
				if (isWeather) {
					GoogleWeatherRequest googleWeather=new GoogleWeatherRequest(listener, reverseContainer.getLocationAddress());
					ServiceManagerImpl.getInstance().submit(googleWeather);
				} else {
					listener.notifyAddress(reverseContainer);
				}

			} else {
				/* launch yahoo reverese coding request */
				YahooReverseCodeRequest yahooReverseCodeReq = new YahooReverseCodeRequest(
						listener, latitude, longitude, false);
				ServiceManagerImpl.getInstance().submit(yahooReverseCodeReq);
			}
		}
	}

	@Override
	public void preExecute() throws Exception {
		Log.i("DBA", "[Google Reverse Code]preExecute");
		httpServiceHelper = new HttpServiceHelper(new URI(
				ApplicationConstants.URL_GOOGLE_REVERSE_CODING));
		List<NameValuePair> requestParameter = new ArrayList<NameValuePair>();
		NameValuePair param = new BasicNameValuePair("q", String
				.valueOf(latitude)
				+ ',' + String.valueOf(longitude));
		requestParameter.add(param);
		param = new BasicNameValuePair("output", "xml");
		requestParameter.add(param);
		param = new BasicNameValuePair("sensor", "true");
		requestParameter.add(param);
		param = new BasicNameValuePair("key",
				ApplicationConstants.API_KEY_GOOGLE);
		requestParameter.add(param);
		httpServiceHelper.setRequestParameter(requestParameter);
	}
}
